package com.snjx.admui.master.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.snjx.admui.master.entity.FileEntity;
import com.snjx.admui.utils.ApiVersion;

import lombok.extern.log4j.Log4j;

/**
* @author zyl
* @date 2017年6月14日 
* @describe
*/
@RestController
@RequestMapping("/{version}/")
@Log4j(topic="defined")
public class FileController {
	/*@Autowired
	private  FileService fileService;*/
    @RequestMapping(value ="/file" ,method = RequestMethod.GET)
    @ApiVersion(value = "")
	   public FileEntity FileInit(){
		log.info("@开始"+new Date());
		FileEntity  fileEntity=new FileEntity();
		return fileEntity;
	 }
     @RequestMapping(value ="/file{fileId}", method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
     @ApiVersion(value = "")
	   public void FileShow(HttpServletRequest request, HttpServletResponse response,@PathVariable("fileId") String fileId) throws UnsupportedEncodingException{
		 log.info("@开始"+new Date());
		 response.setCharacterEncoding("utf-8");
		 JSONObject json = new JSONObject();
		 PrintWriter out = null;
		 try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		FileEntity fileEntity=new FileEntity();
		 String uuid =UUID.randomUUID().toString();
		//根据fileId  查询到 filePath
        if (fileId!=null&&!"".equals(fileId)) {
        	/* fileEntity=this.fileService.findFileById(fileId);*/
        	 
        	 fileEntity.setFileId(uuid);
        	 fileEntity.setFilePath("C:\\Users\\Public\\Videos\\Sample Videos");
        	 json.put("msg", "success");
		     json.put("fileData", fileEntity);
		}
        else{
        	json.put("msg", "error");
        }
       // 输出json
     	    out.write(json.toString());
     	    out.close();
     	   log.info("@结束"+new Date());
	 }
}
